package com.chenl.consumer.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

public class MDCUtils {
    public static String mdc(){
        RequestAttributes requestAttributes= RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=(HttpServletRequest)requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        String traceId;
        String traceIdKey="traceId";
        if (request.getHeader(traceIdKey)==null){
            traceId= UUID.randomUUID().toString().replaceAll("-","");
        }else {
            traceId=request.getHeader("traceIdKey");
        }
        return traceId;
    }
}